package com.xframe.xdal.core.model.es;

import com.xframe.xdal.core.model.ConditionValue;
import com.xframe.xdal.core.util.StringUtil;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

public class MatchQueryBuilder extends BaseQueryBuilder{


    /**
     * 构造函数
     *
     * @param esQueryBuildArgs 构造参数
     */
    public MatchQueryBuilder(String esQueryBuildArgs) {
        super(esQueryBuildArgs);
    }

    @Override
    public QueryBuilder builder(ConditionValue conditionValue) {

        if(StringUtil.isEmpty(this.esQueryBuildArgs)) {
            QueryBuilders.matchQuery(conditionValue.getFieldName(),
                    conditionValue.getValByEs());
        }
        QueryBuilder queryBuilder = null;
        switch (this.esQueryBuildArgs.toLowerCase()){
            case "matchphrase":
                queryBuilder = QueryBuilders.matchPhraseQuery(conditionValue.getFieldName(),
                        conditionValue.getValByEs());
                break;
            default:
                queryBuilder = QueryBuilders.matchQuery(conditionValue.getFieldName(),
                        conditionValue.getValByEs());
                break;
        }
        return queryBuilder;
    }
}
